<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>菜单测试</title>
    <style>
        body { font-family: monospace; padding: 20px; }
        .success { color: green; }
        .error { color: red; }
        pre { background: #f5f5f5; padding: 10px; border-radius: 5px; }
    </style>
</head>
<body>
    <h1>菜单测试页面</h1>

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    echo "<h2>步骤 1: 加载框架</h2>";

    try {
        require __DIR__ . '/../vendor/autoload.php';
        echo "<p class='success'>✓ autoload.php 加载成功</p>";

        $app = new think\App();
        echo "<p class='success'>✓ App 实例创建成功</p>";

        $app->initialize();
        echo "<p class='success'>✓ App 初始化成功</p>";

    } catch (Exception $e) {
        echo "<p class='error'>❌ 框架加载失败: " . $e->getMessage() . "</p>";
        echo "<pre>" . $e->getTraceAsString() . "</pre>";
        exit;
    }

    echo "<h2>步骤 2: 查询数据库菜单</h2>";

    try {
        $dbMenus = \think\admin\model\SystemMenu::mk()
            ->where(['status' => 1, 'pid' => 0])
            ->order('sort desc, id asc')
            ->select()
            ->toArray();

        echo "<p class='success'>✓ 数据库查询成功，找到 " . count($dbMenus) . " 个一级菜单</p>";

        echo "<pre>";
        foreach ($dbMenus as $menu) {
            echo "{$menu['title']} (ID:{$menu['id']}, SORT:{$menu['sort']})\n";
        }
        echo "</pre>";

    } catch (Exception $e) {
        echo "<p class='error'>❌ 数据库查询失败: " . $e->getMessage() . "</p>";
        echo "<pre>" . $e->getTraceAsString() . "</pre>";
    }

    echo "<h2>步骤 3: 检查用户登录状态</h2>";

    try {
        echo "<pre>";
        echo "是否已登录: " . (\think\admin\service\AdminService::isLogin() ? '是' : '否') . "\n";

        if (\think\admin\service\AdminService::isLogin()) {
            echo "用户名: " . \think\admin\service\AdminService::getUserName() . "\n";
            echo "是否超级管理员: " . (\think\admin\service\AdminService::isSuper() ? '是' : '否') . "\n";
        } else {
            echo "当前未登录，请先登录后台\n";
        }
        echo "</pre>";

    } catch (Exception $e) {
        echo "<p class='error'>❌ 检查登录状态失败: " . $e->getMessage() . "</p>";
    }

    echo "<h2>步骤 4: 获取菜单树</h2>";

    try {
        $menus = \think\admin\service\MenuService::getTree();

        echo "<p class='success'>✓ 获取菜单成功，返回 " . count($menus) . " 个一级菜单</p>";

        echo "<pre>";
        foreach ($menus as $menu) {
            echo "├─ {$menu['title']} (ID:{$menu['id']})\n";

            if (!empty($menu['sub'])) {
                foreach ($menu['sub'] as $sub) {
                    echo "│  ├─ {$sub['title']} (ID:{$sub['id']})\n";

                    if (!empty($sub['sub'])) {
                        foreach ($sub['sub'] as $subsub) {
                            echo "│  │  └─ {$subsub['title']} (ID:{$subsub['id']})\n";
                        }
                    }
                }
            }
        }
        echo "</pre>";

        // 检查是否有"系统管理"
        $hasSysMenu = false;
        foreach ($menus as $menu) {
            if ($menu['title'] === '系统管理' || $menu['id'] == 1) {
                $hasSysMenu = true;
                echo "<p class='success'>✓ 找到了系统管理菜单！</p>";
                break;
            }
        }

        if (!$hasSysMenu) {
            echo "<p class='error'>❌ 没有找到系统管理菜单！菜单被过滤掉了</p>";
        }

    } catch (Exception $e) {
        echo "<p class='error'>❌ 获取菜单失败: " . $e->getMessage() . "</p>";
        echo "<pre>" . $e->getTraceAsString() . "</pre>";
    }

    ?>

    <hr>
    <p><a href="javascript:location.reload()">刷新页面</a> | <a href="/admin">返回后台</a></p>
</body>
</html>

